package org.phalanx.messenging;


public class AuthenticationMessage extends AbstractMessage {
	public static final String AUTH_REQUEST = "AUTH_REQUEST";
	public static final String LOGIN_REQUEST = "LOGIN_REQUEST";
	public static final String LOGIN_OK = "LOGIN_OK";
	public static final String LOGIN_DENIED = "LOGIN_DENY";
	public static final String AUTH_DENIED = "AUTH_DENY";
	public static final String AUTH_OK = "AUTH_OK";
	public static final String ALREADY_AUTH = "ALREADY_AUTH";
	public static final String ALREADY_LOGIN = "ALREADY_LOGIN";
	public static final String NOT_AUTH = "NOT_AUTH";
	public static final String NOT_LOGIN = "NOT_LOGIN";
	public static final Integer REASON_BANNED = 0;
	public static final Integer REASON_MAX_FULL = 1;
	public static final Integer REASON_PRIVATE = 2;
	public static final Integer REASON_NONE = -1;
	
	public AuthenticationMessage(){
		super.setMessageHeader("Authentication");
	}
	
	// Actions are set by the client to request either membership
	// or to login to a server
	public void setAction(String action){
		this.setToMessage("ACTION", action);
	}
	
	public String getAction(){
		return (String)this.getFromMessage("ACTION");
	}
	
	// Permissions are set by the server in response to a client action
	// such as requesting membership or Login to a server
	public void setRequest(String request){
		this.setToMessage("PERMISSION", request);
	}
	
	public String getRequest(){
		return (String)this.getFromMessage("PERMISSION");
	}
	
	
	// Reasons are set in the message by the server to inform the client
	// why they have been denied either authentication or login
	// For simplicity sake these are set as Integer values when they are
	// retrieved or set and the client(s) can translate these messages
	// In human readable form. The default is REASON_NONE
	public Integer getReason(){
		Integer reason = REASON_NONE;
		String stringReason = (String)this.getFromMessage("REASON");
		try {
			if (stringReason != null){
				reason = Integer.valueOf(stringReason);
			}
		} catch (NumberFormatException nfe){}
		
		return reason;
	}
	
	public void setReason(Integer newReason){
		String stringReason = Integer.toString(REASON_NONE);
		
		if (newReason != null){
			stringReason = newReason.toString();
		}
		
		this.setToMessage("REASON", stringReason);
	}
}
